package org.lyimg.com.imgapplication.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;

import org.lyimg.com.imgapplication.R;
import org.lyimg.com.imgapplication.activity.ImgShowActivity;
import org.lyimg.com.imgapplication.activity.ImgsActivity;
import org.lyimg.com.imgapplication.bean.ImgList;
import org.lyimg.com.imgapplication.bean.ImgType;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by liuyong on 2017/4/19.
 */

public class ImgRecAdapter extends RecyclerView.Adapter<ImgRecAdapter.MyViewHolder> {

    Context context;
    List<ImgList> list;
    private List<Integer> heightList;//装产出的随机数
    String referer;

    public ImgRecAdapter(Context context, List<ImgList> list, String referer) {
        this.context = context;
        this.list = list;
        //记录为每个控件产生的随机高度,避免滑回到顶部出现空白
        heightList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            int height = new Random().nextInt(250) + 100;//[100,300)的随机数
            heightList.add(height);
        }
        this.referer = referer;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_imgs, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.tv_name.setText(list.get(position).title);
        String img_url = list.get(position).img;

//        Log.i("TAG", "onBindViewHolder: " + img_url);
        Glide.with(context).load(buildGlideUrl(img_url)).diskCacheStrategy(DiskCacheStrategy.ALL).fitCenter()
                .crossFade().into(holder.img);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv_name, tv_city, tv_height, tv_weight;
        ImageView img;

        public MyViewHolder(View view) {
            super(view);
            tv_name = (TextView) view.findViewById(R.id.tv_name);
            tv_city = (TextView) view.findViewById(R.id.tv_city);
            tv_height = (TextView) view.findViewById(R.id.tv_height);
            tv_weight = (TextView) view.findViewById(R.id.tv_weight);
            img = (ImageView) view.findViewById(R.id.img);

            img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, ImgShowActivity.class);
                    intent.putExtra("link", list.get(getPosition()).link);
                    context.startActivity(intent);
                }
            });

        }
    }

    private GlideUrl buildGlideUrl(String url) {
        if (TextUtils.isEmpty(url)) {
            return null;
        } else {
            return new GlideUrl(url, new LazyHeaders.Builder()
                    .addHeader("Referer", referer)
                    .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
                    .addHeader("Accept-Encoding", "gzip, deflate, sdch, br")
                    .addHeader("Accept-Language", "zh-CN,zh;q=0.8")
                    .addHeader("Cache-Control", "max-age=0")
                    .addHeader("Connection", "keep-alive")
                    .addHeader("If-None-Match", "5c45772c-562c")
                    .addHeader("Upgrade-Insecure-Requests", "1")
                    .addHeader("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1")
                    .build());
        }
    }
}
